package com.bawei.mvvm.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.mvvm.R;
import com.bawei.mvvm.databinding.ItemBinding;
import com.bawei.mvvm.entity.Goods;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.GoodsViewHoler> {
    private List<Goods> mGoodsList;

    public GoodsAdapter(List<Goods> goodsList) {
        mGoodsList = goodsList;
    }

    @NonNull
    @Override
    public GoodsViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ItemBinding itemBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item, parent, false);
        return new GoodsViewHoler(itemBinding);
    }
    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull GoodsViewHoler holder, int position) {
        holder.mItemBinding.setGoods(mGoodsList.get(position));
    }

    @Override
    public int getItemCount() {
        return mGoodsList.size();
    }

    class GoodsViewHoler extends RecyclerView.ViewHolder{
        ItemBinding mItemBinding;

        public GoodsViewHoler( ItemBinding itemBinding) {
            super(itemBinding.getRoot());
            mItemBinding = itemBinding;
        }

        public GoodsViewHoler(@NonNull View itemView) {
            super(itemView);
        }
    }
}
